package sdfi.lee.DAO;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import sdfi.lee.Entity.Platform;

public class PlatDAO {
	public String flatAdd(Platform plat)
	{
		
		Session session=null;
		Transaction ts=null;
		try
		{
			session=DAO.openSession();
			ts=session.beginTransaction();
			session.save(plat);
			ts.commit();
			return "success";
		}
		catch(Exception ee)
		{
			
			return "error";
		}
		finally
		{
			DAO.closeSession();
		}
	}

	public List<Platform> getPlatform(int size,int page)
	{
		List<Platform> list=null;
		Session session=null;
		Transaction ts=null;
		try
		{
			session=DAO.openSession();
			ts=session.beginTransaction();
			Query query=session.createQuery("from Platform");
			query.setFirstResult((page-1)*size);
			query.setMaxResults(size);
			list=query.list();
			ts.commit();
			
			
		}
		catch(Exception ee)
		{
			System.out.println("");
			ee.printStackTrace();
			if(ts!=null)ts.rollback();
		}
		finally
		{
			DAO.closeSession();
		}
		return list;
	}
	public Platform getPlatbyId(Integer id)
	{
		Platform plat=null;
		Session session=null;
		Transaction ts=null;
		try
		{
			session=DAO.openSession();
			ts=session.beginTransaction();
			plat=(Platform)session.load(Platform.class, id);
			ts.commit();
			
		}
		catch(Exception ee)
		{
			System.out.println("Exception when get Plat by id");
			ee.printStackTrace();
		}
		finally
		{
			DAO.closeSession();
		}
		return plat;
	}
	public String modify(Platform plat)
	{
		Session session=null;
		Transaction ts=null;
		try
		{
			session=DAO.openSession();
			ts=session.beginTransaction();
			Platform old=(Platform)session.get(Platform.class, plat.getId());
			old.setPlatcount(plat.getPlatcount());
			old.setPlatdescribe(plat.getPlatdescribe());
			old.setPlatname(plat.getPlatname());
			old.setPlattoll(plat.getPlattoll());
			old.setPlattollfmt(plat.getPlattollfmt());
			session.update(old);
			ts.commit();
			return "success";
		}
		catch(Exception ee)
		{	
			ts.rollback();
			ee.printStackTrace();
			return "error";
		}
		finally
		{
			DAO.closeSession();
		}
		
	}
}
